package com.awenhui.demo.module.security;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

/**
 * 自定义成功处理器
 *
 * @author yuxf
 * @version 1.0
 * @date 2020/10/26 10:18
 */
@Component
public class CustomAuthenticationSucessHandler extends SimpleUrlAuthenticationSuccessHandler {

    @Autowired
    public ObjectMapper objectMapper;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        response.setContentType("applicantion/json;charset=utf-8");
        HashMap<String, Object> map = new HashMap<>();
        map.put("success", true);
        map.put("code", 0);
        map.put("message", "登录成功");
        String result = objectMapper.writeValueAsString(map);
        response.getWriter().write(result);
    }
}
